package cn.innosmart.aq.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;

import cn.innosmart.aq.R;
import cn.innosmart.aq.bean.Employee;

/**
 * Created by David on 2016/1/12.
 */
public class EmployeeAdapter extends BaseAdapter {

    public boolean isModify;
    private ArrayList<Employee> employees;
    private Context context;
    private LayoutInflater mInflater;
    private View.OnClickListener onDeleteClickListener;

    /**
     * 指派显示员工的适配器
     * @param context
     * @param employees
     */
    public EmployeeAdapter(Context context, ArrayList<Employee> employees) {
        this.context = context;
        this.mInflater = LayoutInflater.from(context);
        this.employees = employees;
    }

    @Override
    public int getCount() {
        return employees.size();
    }

    @Override
    public Object getItem(int position) {
        return employees.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.listview_employee, null);
            holder.rlDelete = (RelativeLayout) convertView.findViewById(R.id.rl_delete);
            holder.tvDelete = (TextView) convertView.findViewById(R.id.tv_delete);
            holder.tvNickName = (TextView) convertView.findViewById(R.id.tv_nickname);
            AutoUtils.autoSize(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Employee employee = employees.get(position);
        holder.tvNickName.setText(employee.nickName);
        if (isModify) {
            holder.rlDelete.setVisibility(View.VISIBLE);
            holder.rlDelete.setTag(employee);
            if (onDeleteClickListener != null)
                holder.rlDelete.setOnClickListener(onDeleteClickListener);
        } else
            holder.rlDelete.setVisibility(View.GONE);
        return convertView;
    }

    /**
     * 进入修改模式
     * @param startModify
     */
    public void startModify(boolean startModify) {
        this.isModify = startModify;
        notifyDataSetChanged();
    }

    /**
     * 设置删除的点击事件
     * @param onDeleteClickListener
     */
    public void setOnDeleteClickListener(View.OnClickListener onDeleteClickListener) {
        this.onDeleteClickListener = onDeleteClickListener;
    }

    /**
     * 刷新员工列表界面
     * @param employees
     */
    public void refresh(ArrayList<Employee> employees) {
        this.employees = employees;
        notifyDataSetChanged();
    }

    static class ViewHolder {
        TextView tvNickName;
        TextView tvDelete;
        RelativeLayout rlDelete;
    }
}
